<template>
	<view class="">
		<NavBar :title="title" :autoBack="false" id="nav">
			<template #left>
				<view class="navbar-left center" @click="goBack">
					<u-icon name="arrow-left" color="#007aff" size="34"></u-icon>
				</view>
			</template>
			<template #right>
				<view class="navbar-right center exprot-btn" @click="exprotPdf">
					导出pdf
				</view>
			</template>
		</NavBar>
		<view class="block100"></view>
		<view class="content">
			<view class="navbox">
				<u-subsection :list="tabsList" bg-color="#fff" button-color="#fff" v-model="activeIndex"
					@change="changeNav"></u-subsection>
			</view>
			<view class="block30"></view>
			<view class="drop-container">
				<view v-for="(item,index) in showData" :key="index">
					<view class="questions">
						<DtailEachQuestion :question="item">
						</DtailEachQuestion>
					</view>
				</view>
				<view class="footer">
					<view class="btn" v-if="+activeIndex!==0" @click.stop="toPre">
						上一步
					</view>
					<view class="btn" v-if="+activeIndex!==2" @click.stop="toNext">
						下一步
					</view>
				</view>
			</view>

		</view>

	</view>
</template>

<script>
	import DtailEachQuestion from './components/DtailEachQuestion.vue'
	import NavBar from "@/component/NavBar"
	import {
		downLoadFile
	} from "@/utils/util/uploadFun.js"
	// import {
	// 	detailData
	// } from './data.js'
	import {
		getTemplateResult,
		getDetailFormTemplateDetail,
		getTemplatePdf
	} from "@/api/template.js"
	export default {
		name: 'answer',
		components: {
			DtailEachQuestion,
			NavBar
		},
		computed: {
			showData() {
				return this[this.tabName]
			}
		},
		data() {
			return {
				title: '查看檢查表',
				frontendData: [],
				reviewData: [],
				achievementsData: [],
				tabName: 'frontendData',
				tabsList: [{
						name: '前置工作',
						index: 1,
						tabName: 'frontendData'
					},
					{
						name: '工作檢核',
						index: 2,
						tabName: 'reviewData'
					},
					{
						name: '接績',
						index: 3,
						tabName: "achievementsData"
					},
				],
				activeIndex: 0,
				casezy_form_template_result_id: '',
				onTemplate: false, // 代表是從檢查表庫過來的，需要查另外一個詳情
				id: ''
			}
		},
		// created() {
		// 	this.frontendData = detailData.frontendData
		// 	this.reviewData = detailData.reviewData
		// 	this.achievementsData = detailData.achievementsData
		// },
		onLoad() {
			if (!uni.getStorageSync("islogin")) {
				this.$Toast("請先登錄")
				setTimeout(() => {
					this.toPage("Home")
				}, 1200)
			} else {
				const {
					casezy_form_template_result_id,
					onTemplate,
					id
				} = this.$Route.query
				this.onTemplate = !!onTemplate
				this.id = id
				this.casezy_form_template_result_id = casezy_form_template_result_id
				if (this.onTemplate) {
					this.getTemDetail()
				} else {
					this.getDetail()
				}
			}
		},
		methods: {
			goBack() {
				uni.navigateBack(-1)
			},
			changeNav(index) {
				console.log(index)
				this.tabName = this.tabsList[index].tabName
			},
			toNext() {
				switch (this.tabName) {
					case 'frontendData':
						this.tabName = 'reviewData'
						this.activeIndex++
						console.log(this.frontendData)
						break;
					case 'reviewData':
						this.tabName = 'achievementsData'
						this.activeIndex++
						break;
				}
				this.$goTop()
			},
			toPre() {
				switch (this.tabName) {
					case 'reviewData':
						this.tabName = 'frontendData'
						break;
					case 'achievementsData':
						this.tabName = 'reviewData'
						break;
				}
				this.$goTop()
				this.activeIndex--
			},
			getDetail() {
				let data = {
					casezy_form_template_result_id: this.casezy_form_template_result_id,
					uid: uni.getStorageSync("uid"),
				}
				getTemplateResult(data).then((res) => {
					try {
						let resData = res.form_template_result
						let obj = JSON.parse(resData)
						this.form_template = res.form_template
						this.frontendData = obj.frontendData
						this.achievementsData = obj.achievementsData
						this.reviewData = obj.reviewData
					} catch (err) {
						console.log(err)
					}
				}).catch((err) => {
					if (err.code === 102) {
						this.$ClearLogin(err.msg)
					} else {
						this.$Toast(err.msg)
					}
				})
			},
			// 從檢查表庫過來查這個詳情
			getTemDetail() {
				let data = {
					id: this.id,
					uid: uni.getStorageSync("uid"),
				}
				getDetailFormTemplateDetail(data).then((resData) => {
					try {
						let obj = JSON.parse(resData.form_template_content)
						this.frontendData = obj.frontendData
						this.achievementsData = obj.achievementsData
						this.reviewData = obj.reviewData
					} catch (err) {
						console.log(err)
					}
				}).catch((err) => {
					if (err.code === 102) {
						this.$ClearLogin(err.msg)
					} else {
						this.$Toast(err.msg)
					}
				})
			},
			exprotPdf() {
				let uid = uni.getStorageSync("uid")
				let data = {
					uid,
					casezy_form_template_result_id: this.casezy_form_template_result_id,
				}
				uni.showLoading({
					title: '生成中',
					duration: 2000000
				});
				getTemplatePdf(data).then((res) => {
					downLoadFile(res.url)
					uni.hideLoading();
				}).catch((err) => {
					if (err.code === 102) {
						this.$ClearLogin(err.msg)
					} else {
						this.$Toast(err.msg)
					}
				})
			},
		},

	}
</script>

<style lang="scss" scoped>
	.block100 {
		height: 100rpx;
	}

	.block30 {
		height: 100rpx;
	}

	.tabs {
		display: flex;
		justify-content: space-around;

		.tab {
			width: 360rpx;
			height: 64rpx;
			line-height: 64rpx;
			font-weight: 400;
			color: #333333;
			margin-bottom: 16rpx;
			text-align: center;
			cursor: pointer;
		}

		.active {
			background: #FFFFFF;
			box-shadow: 0rpx 2rpx 2rpx 1rpx rgba(197, 197, 197, 0.25);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
		}
	}

	.content {
		margin: 0 auto;
		max-width: 1800rpx;
		position: relative;

		.navbox {
			position: absolute;
			top: 0rpx;
			left: 0;
			width: 100%;

			/deep/ .u-item {
				position: relative;

				&::after {
					content: "";
					width: 2rpx;
					height: 32rpx;
					background-color: #f1f1f1;
					position: absolute;
					right: 0rpx;
				}

				&.u-item-3::after {
					display: none;
				}
			}

			/deep/ .u-item-bg {
				box-shadow: 0rpx 4rpx 4rpx rgba(0, 0, 0, .1);
			}
		}

		.drop-container {
			border: 1px solid #F6F6F6;
			border-radius: 10rpx;
		}
	}

	.navbox {
		margin: 0 auto;

		/deep/ .u-item {
			position: relative;

			&::after {
				content: "";
				width: 2rpx;
				height: 32rpx;
				background-color: #f1f1f1;
				position: absolute;
				right: 0rpx;
			}

			&.u-item-3::after {
				display: none;
			}
		}

		/deep/ .u-item-bg {
			box-shadow: 0rpx 4rpx 4rpx rgba(0, 0, 0, .1);
		}
	}

	.footer {
		display: flex;
		justify-content: flex-end;
		padding: 0 24rpx;
		gap: 24rpx;
		margin-top: 24rpx;

		.btn {
			color: #fff;
			cursor: pointer;
			background: #3A78FC;
			border-radius: 6px;
			padding: 12rpx 24rpx;
		}
	}

	::v-deep .u-radio__icon-wrap--disabled--checked {
		color: #2979ff !important;

		.uicon-checkbox-mark {
			&:before {
				color: #2979ff !important;
			}
		}
	}

	::v-deep .u-checkbox__icon-wrap--disabled--checked {
		color: #2979ff !important;

		.uicon-checkbox-mark {
			&:before {
				color: #2979ff !important;
			}
		}
	}

	.exprot-btn {
		cursor: pointer;
		color: #2979ff;
	}
</style>